const { regeneratorRuntime, dayjs } = global

Page(global.connect(
  state => ({
    ...state.globalReducer,
    ...state.activityReducer
  }),
  dispatch => ({
    async asyncAdsPostionSettingData(data) {
      return dispatch(global.activityReducer.asyncAdsPostionSettingData(data))
    },
    async asyncPositionNum(data) {
      return dispatch(global.activityReducer.asyncPositionNum(data))
    },
  })
)({
  onLoad(option) {
    console.log(this.route)
    this.setData({
      positionIndex: option.positionIndex,
      // tplCount: storePosition.data[option.positionIndex].num
    })
  },
  handleMovCount(e) {
    const { positionIndex } = this.data
    let { num = 1 } = this.data.storePosition.data[positionIndex].tpl
    num = Number(num)
    num -= 1
    if (num > 0) {
      this.asyncPositionNum({
        index: this.data.positionIndex,
        num
      })
    }
  },
  handleAddCount(e) {
    const { positionIndex } = this.data
    let { num = 1 } = this.data.storePosition.data[positionIndex].tpl
    num = Number(num)
    num += 1
    if (num > 0) {
      this.asyncPositionNum({
        index: this.data.positionIndex,
        num
      })
    }
  },
  handleLicon1Tap() {
    wx.navigateBack()
  },
  handleRoute(e) {
    console.log('handleRoute', e)
    const { storePosition, positionIndex } = this.data
    const positionDetail = storePosition.data[positionIndex]
    const { index } = e.currentTarget.dataset

    console.log(positionDetail.tpl.detail[index])

    // 前往广告块编辑页 系统广告位
    if (positionDetail.ads_type == 1) {
      const blockInfo = positionDetail.tpl.detail[index]
      blockInfo.index = index
      blockInfo.positionIndex = positionIndex
      this.asyncAdsPostionSettingData(blockInfo)
      global.handleRoute({
        route: 'mos-step2'
      })
    }

    // if (positionDetail.ads_type === 2) {
    //   this.initCustomAdsPositionData({
    //     ...this.data.orderDetail.data.ads_info[index]
    //   })
    //   global.handleRoute({ route: 'effect-custom' })
    // } else {
    //   this.asyncAdsPostionSettingData({ index })
    //   global.handleRoute({ route: 'mos-step1' })
    // }
  },
  handleNextStep(e) {
    console.log('handleNextStep', e)
    const { storePosition, positionIndex } = this.data
    const positionDetail = storePosition.data[positionIndex]
    const flag = positionDetail.tpl.detail.reduce((prev, next) => prev && next.image_id && next.materiel_id, true)
    if (!flag) {
      this.selectComponent('#toast').handleShow({
        message: '请完善广告位信息'
      })
      return
    }
    wx.navigateBack()
  },
}))